package com.example.restaurantclient.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.restaurantclient.Adapter.CustomRestaurantAppointmentAdapter;
import com.example.restaurantclient.Entities.Restaurant;
import com.example.restaurantclient.R;
import com.example.restaurantclient.WheelDialog.HeightWheelDialog;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;
import com.wdullaer.materialdatetimepicker.time.TimePickerDialog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class AppointmentListActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener{
    private TextView peopleNum;
    private LinearLayout linearPeopleNum;
    private HeightWheelDialog mHeight_Dialog1;
    private HeightWheelDialog.HeightWheelDialogcallback mCallback_height1;
    private ListView listView;
    private List<Restaurant> restaurantsItems=new ArrayList<>();
    private TextView appointDate;
    private TextView appointWeek;
    private TextView appointTime;
    private LinearLayout sumDateLinearlayout;
    DatePickerDialog datePickerDialog ;
    TimePickerDialog timePickerDialog ;
    int Year, Month, Day, Hour, Minute;
    private SearchView searchView;
    Calendar calendar ;
    public static String[] WEEK = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appointment);

        Toast.makeText(AppointmentListActivity.this, "请在上方选择预定时间！", Toast.LENGTH_SHORT).show();

        calendar = Calendar.getInstance();
        Year = calendar.get(Calendar.YEAR) ;
        Month = calendar.get(Calendar.MONTH);
        Day = calendar.get(Calendar.DAY_OF_MONTH);
        Hour = calendar.get(Calendar.HOUR_OF_DAY);
        Minute = calendar.get(Calendar.MINUTE);
        peopleNum=findViewById(R.id.text_peoplenum);
        linearPeopleNum=findViewById(R.id.people_linear_num);
        listView=findViewById(R.id.house_list);
        appointDate=findViewById(R.id.appointment_date);
        appointWeek=findViewById(R.id.appointment_week);
        appointTime=findViewById(R.id.appointment_time);
        sumDateLinearlayout=findViewById(R.id.appointment_sum_date);
        searchView = findViewById(R.id.cookie_manage_searchViewa);

        initDatePicker();
        linearPeopleNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                height_Dialog_Display1();
            }
        });

        mCallback_height1 = new HeightWheelDialog.HeightWheelDialogcallback() {
            @Override
            public void dialogdo(String string, Dialog dialog) {
                peopleNum.setText(string);
            }
        };
        //新建数据
        initData();
        final CustomRestaurantAppointmentAdapter customRestaurantAppointmentAdapter=new CustomRestaurantAppointmentAdapter(AppointmentListActivity.this,restaurantsItems,R.layout.restaurant_appointment_list_item);
        listView.setAdapter(customRestaurantAppointmentAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    switch (query)
                    {
                        case "国泰":
                            restaurantsItems.clear();
                            Restaurant restaurant1=new Restaurant("国泰宏城店",11.3f,"北京市顺义区国泰宏城大卖场F1",4,7,3,"0312-103789",40.12962886207276f,116.63480658743666f,45);
                            restaurantsItems.add(restaurant1);
                            customRestaurantAppointmentAdapter.notifyDataSetChanged();
                            listView.setAdapter(customRestaurantAppointmentAdapter);
                            break;
                        case "新世界":
                            restaurantsItems.clear();
                            Restaurant restaurant2=new Restaurant("新世界店",3.5f,"北京市顺义区新顺南大街新世界百货F1",2,4,1,"0312-156789",40.12689305931414f,116.65163476679609f,21);
                            restaurantsItems.add(restaurant2);
                            customRestaurantAppointmentAdapter.notifyDataSetChanged();
                            listView.setAdapter(customRestaurantAppointmentAdapter);
                            break;
                        case "顺康":
                            restaurantsItems.clear();
                            Restaurant restaurant3=new Restaurant("顺康路店",8.6f,"北京市顺义区顺康路中晟景苑",5,1,4,"0312-102269",40.11238777407324f,116.6749243876724f,32);
                            restaurantsItems.add(restaurant3);
                            customRestaurantAppointmentAdapter.notifyDataSetChanged();
                            listView.setAdapter(customRestaurantAppointmentAdapter);
                            break;
                    }
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    return false;
                }
            });
        }

    }
    //初始化数据
    public void initData(){
        Restaurant restaurant1=new Restaurant("国泰宏城店",11.3f,"北京市顺义区国泰宏城大卖场F1",4,7,3,"0312-103789",40.12962886207276f,116.63480658743666f,45);
        Restaurant restaurant2=new Restaurant("新世界店",3.5f,"北京市顺义区新顺南大街新世界百货F1",2,4,1,"0312-156789",40.12689305931414f,116.65163476679609f,21);
        Restaurant restaurant3=new Restaurant("顺康路店",8.6f,"北京市顺义区顺康路中晟景苑",5,1,4,"0312-102269",40.11238777407324f,116.6749243876724f,32);
        restaurantsItems.add(restaurant1);
        restaurantsItems.add(restaurant2);
        restaurantsItems.add(restaurant3);
    }
    public void height_Dialog_Display1() {
        if (mHeight_Dialog1 == null) {
            mHeight_Dialog1 = new HeightWheelDialog(this, getWindowManager());
            mHeight_Dialog1.setCanceledOnTouchOutside();
        }
        mHeight_Dialog1.setDialogCallback(mCallback_height1);
        mHeight_Dialog1.show();
    }
    public void initDatePicker(){
        //选择日期和星期
        sumDateLinearlayout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                datePickerDialog = DatePickerDialog.newInstance(AppointmentListActivity.this, Year, Month, Day);
                datePickerDialog.setThemeDark(false);
                datePickerDialog.showYearPickerFirst(false);
                datePickerDialog.setTitle("Date Picker");
                datePickerDialog.setAccentColor("#d81e06");


                // Setting Min Date to today date
                Calendar min_date_c = Calendar.getInstance();
                datePickerDialog.setMinDate(min_date_c);



                // Setting Max Date to next 2 years
                Calendar max_date_c = Calendar.getInstance();
                max_date_c.set(Calendar.YEAR, Year+20);
                datePickerDialog.setMaxDate(max_date_c);

//                //Disable all SUNDAYS and SATURDAYS between Min and Max Dates
//                for (Calendar loopdate = min_date_c; min_date_c.before(max_date_c); min_date_c.add(Calendar.DATE, 1), loopdate = min_date_c) {
//                    int dayOfWeek = loopdate.get(Calendar.DAY_OF_WEEK);
//                    if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.SATURDAY) {
//                        Calendar[] disabledDays =  new Calendar[1];
//                        disabledDays[0] = loopdate;
//                        datePickerDialog.setDisabledDays(disabledDays);
//                    }
//                }



                datePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface dialogInterface) {

                        Toast.makeText(AppointmentListActivity.this, "未更改预定日期", Toast.LENGTH_SHORT).show();
                    }
                });

                datePickerDialog.show(getFragmentManager(), "DatePickerDialog");
            }
        });

        //选择时间
        appointTime.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                timePickerDialog = TimePickerDialog.newInstance(AppointmentListActivity.this, Hour, Minute,false );
                timePickerDialog.setThemeDark(false);
                //timePickerDialog.showYearPickerFirst(false);
                timePickerDialog.setTitle("Time Picker");
                timePickerDialog.setAccentColor("#d81e06");
                timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface dialogInterface) {

                        Toast.makeText(AppointmentListActivity.this, "未更改预定时间", Toast.LENGTH_SHORT).show();
                    }
                });

                timePickerDialog.show(getFragmentManager(), "TimePickerDialog");
            }
        });
    }
    //回传日期
    @Override
    public void onDateSet(DatePickerDialog view, int Year, int Month, int Day) {
        int i=getDayofWeek(Year+"-"+(Month+1)+"-"+Day);
        String week=WEEK[i-1];
        String date =Year+"年"+(Month+1)+"月"+Day+"日";

        Toast.makeText(AppointmentListActivity.this, date, Toast.LENGTH_LONG).show();

        appointDate.setText(date);
        appointWeek.setText(week);

    }
    //回传时间
    @Override
    public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {

        String time =hourOfDay+" : "+minute;

        Toast.makeText(AppointmentListActivity.this, time, Toast.LENGTH_LONG).show();

        appointTime.setText(time);
    }
    //将日期转化成星期
    private int getDayofWeek(String dateTime) {
        Calendar cal = Calendar.getInstance();
        if (dateTime.equals("")) {
            cal.setTime(new Date(System.currentTimeMillis()));
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            Date date;
            try {
                date = sdf.parse(dateTime);
            } catch (ParseException e) {
                date = null;
                e.printStackTrace();
            }
            if (date != null) {
                cal.setTime(new Date(date.getTime()));
            }
        }
        return cal.get(Calendar.DAY_OF_WEEK);
    }

}
